﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using cs499.Database;

namespace cs499.Classes
{
    public partial class AddClass : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (HttpContext.Current.Session["AddClassLabeltext"] != null && IsPostBack)
            {
                AddClassLabel.Text = (string)HttpContext.Current.Session["AddClassLabeltext"];
                //OKButton.Enabled = false;
            }
            else if (IsPostBack)
            {
                AddClassLabel.Text = "";
            }
            else {
                AddClassLabel.Text = "";
            }
        }
        
        protected void Page_PreRender(object sender, EventArgs e)
        {
            LinkButton classesMenuButton = (LinkButton)Master.FindControl("LoginView").FindControl("ClassesMenuButton");
            classesMenuButton.CssClass = "selected";

            if (AddClassLabel.Text != "") return;
            if (HttpContext.Current.Session["CurrentDepartmentId"] != null)
            {
                int CurrentDepartmentId = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                cs499.Database.Department CurrentDepartment = ListOfDepartment.GetDepartment(ID: CurrentDepartmentId);
                AddClassLabel.Text = "Input information for new class in The " + CurrentDepartment.Name + " department";
            }
            else
            {
                Response.Redirect("~/Classes/ListClasses.aspx");
            }
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Classes/ListClasses.aspx");
        }

        protected void OKButton_Click(object sender, EventArgs e)
        {
            String strnumber = inputclassnumber.Text;
            String coursetitle = inputclasstitle.Text;
            String strmaxsize = inputmaxsize.Text;
            String strnumsections = inputnumsections.Text;
            int[] rooms = inputlocationrestraints.GetSelectedIndices();
            int number;
            
            int maxenroll;
            int numsections;

            if (!int.TryParse(strnumber, out number))
            {
                String errmsg = "Class Number must be greater than 0";
                if (AddClassLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddClassLabeltext"] = errmsg;
                    Server.Transfer("~/Classes/AddClass.aspx", true);
                }
                return;
            }
            if (!int.TryParse(strmaxsize, out maxenroll))
            {
                String errmsg = "Maximum enrollment must be greater than 0";
                if (AddClassLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddClassLabeltext"] = errmsg;
                    Server.Transfer("~/Classes/AddClass.aspx", true);
                }
                return;
            }
            if (!int.TryParse(strnumsections, out numsections))
            {
                String errmsg = "Number of sections must be greater than 0";
                if (AddClassLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddClassLabeltext"] = errmsg;
                    Server.Transfer("~/Classes/AddClass.aspx", true);
                }
                return;
            }
            
            Department curdep;
            if (HttpContext.Current.Session["CurrentDepartmentId"] == null)
            {
                DropDownList seldep = (DropDownList)Page.Master.FindControl("LoginViewForDepartmentSelection_DepartmentSelection");

                curdep = ListOfDepartment.GetDepartment(Name: seldep.Text);
                if (curdep == null) HttpContext.Current.Session["CurrentDepartmentId"] = null;
                else HttpContext.Current.Session["CurrentDepartmentId"] = curdep.Id;
                HttpContext.Current.Session["CurrentDepartmentselectedindex"] = seldep.SelectedIndex;
            }
            else {
                int idtocheckfor = (int)HttpContext.Current.Session["CurrentDepartmentId"];
                curdep = ListOfDepartment.GetDepartment(ID: idtocheckfor);
            }
            
            if (curdep.GetCourse(number: number)!=null) {
                String errmsg = "Class already exists in the " + curdep.Name + " department";
                if (AddClassLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddClassLabeltext"] = errmsg;
                    Server.Transfer("~/Classes/AddClass.aspx", true);
                }
                return;
            }
            
            int[] locationrestraints = inputlocationrestraints.GetSelectedIndices();
            int[] PlaceIDs = null;
            if(locationrestraints.Length > 0){
                List<int> tempPlaceIDs = new List<int>();
                foreach (int idx in locationrestraints)
                {
                    tempPlaceIDs.Add(Convert.ToInt32(inputlocationrestraints.Items[idx].Value));
                }
                PlaceIDs = tempPlaceIDs.ToArray();
            }
            Course newCourse = curdep.CreateCourse(coursetitle, number, numsections, maxenroll, PlaceIDs);
            if(newCourse==null)
            {
                String errmsg = "Unknown Error while creating course";
                if (AddClassLabel.Text != errmsg)
                {
                    HttpContext.Current.Session["AddClassLabeltext"] = errmsg;
                    Server.Transfer("~/Classes/AddClass.aspx", true);
                }
                return;
            }

            
            HttpContext.Current.Session["AddClassLabeltext"] = "";
            AddClassLabel.Text = "Success";
            Response.Redirect("~/Classes/ListClasses.aspx?action=success");
        }
    }
}